package edu.stanford.hardon;

import edu.stanford.hardon.Tabs.AudioTab;
import edu.stanford.hardon.Tabs.CameraTab;
import edu.stanford.hardon.Tabs.NotesTab;
import edu.stanford.hardon.Tabs.ManageTab;
import edu.stanford.hardon.Tabs.TabHandler;
import edu.stanford.hardon.Tabs.VideoTab;

import android.app.Activity;
import android.content.Intent;
import android.content.Context;
import android.content.res.Configuration;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

import android.os.Bundle;

import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class TravelHard extends Activity {
	
	private static final int TAB_AUDIO = 1;
	private static final int TAB_NOTES = 2;
	private static final int TAB_CAMERA = 3;
	private static final int TAB_VIDEO = 4;
	private static final int TAB_MANAGE = 5;
	
	private int currentTab = 1;
	private TabHandler mTabs;
	public Location location;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
 
        // need to do this BEFORE initializing tabs so we can pass a pointer
        LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		LocationListener mlocListener = new MyLocationListener();	
		mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 
		
		// Get an immediate location update
		location = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		if (location == null) {
			Log.i("LOCATION", "The location is still null!");
		} else {
			Log.i("LOCATION", "Lat: " + location.getLatitude() + ", Lng: " + location.getLongitude() + ", Alt: " + location.getAltitude());
		}
        
        mTabs = (TabHandler) this.findViewById( R.id.Tabs );
        
        mTabs.addTabMember( new AudioTab( TAB_AUDIO, "Audio", R.drawable.microphone, (LinearLayout) this.findViewById( R.id.TabLayout_One ), this) );
        mTabs.addTabMember( new NotesTab( TAB_NOTES, "Notes", R.drawable.notes,(LinearLayout) this.findViewById( R.id.TabLayout_Two ), this ) );
        mTabs.addTabMember( new CameraTab( TAB_CAMERA, "Camera", R.drawable.camera,(LinearLayout) this.findViewById( R.id.TabLayout_Four ) ,(SurfaceView) this.findViewById(R.id.camera_surface), this ) );
        mTabs.addTabMember( new VideoTab( TAB_VIDEO, "Video", R.drawable.video,(LinearLayout) this.findViewById( R.id.TabLayout_Five ) ,(SurfaceView) this.findViewById(R.id.video_surface), this ) );
        mTabs.addTabMember( new ManageTab( TAB_MANAGE, "Manage", R.drawable.manage, (LinearLayout) this.findViewById( R.id.TabLayout_Three ), this ) );

        if(savedInstanceState != null &&  savedInstanceState.containsKey("currentTab")){
        	int tabId = savedInstanceState.getInt("currentTab");
        	currentTab = tabId;
         	mTabs.setActiveTab(tabId);
        }
        else{
        	mTabs.setActiveTab(TAB_CAMERA);
        	this.findViewById(R.id.TabLayout_Four).setVisibility(View.VISIBLE);
        }
        
        // start the Upload Service to upload media to server
        startService(new Intent(this, UploadService.class)); 
    }
    
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.i("TAB", String.valueOf(mTabs.getActiveTabNum()));
		if(keyCode == KeyEvent.KEYCODE_BACK){
			if(mTabs.getActiveTab().onBackButtonDown()){
				return true;
			}
			else{
				return super.onKeyDown(keyCode, event);
			}
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}
	
	public void onResume() {
		super.onResume();
		Log.d("camera","on resume");
		mTabs.resumeCurrent();
	}
		
	public void onPause() {
		Log.d("camera","on pause");
		mTabs.pauseCurrent();
		super.onPause();
	}
	
	public void onConfigurationChanged(Configuration newConfig) {
	    super.onConfigurationChanged(newConfig);
	    Log.d("hio","onConfigChanged called");
	}
	
	public void onSaveInstanceState(Bundle b){
		Log.d("hio","on save Instance State Bundle");
		b.putInt("currentTab", currentTab);
	}
	
	   /* Class My Location Listener */
	public class MyLocationListener implements LocationListener	{
	
		public void onLocationChanged(Location loc) {
			location = loc;
		}
		
		public void onProviderDisabled(String provider) {
			Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
		}
		
		public void onProviderEnabled(String provider) {
			Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
		}
		
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
		
	}/* End of Class MyLocationListener */
	
}